package com.wms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.bean.PageBean;
import com.wms.constant.Prefix;
import com.wms.entity.Purchase;
import com.wms.entity.PurchaseDetail;
import com.wms.entity.PurchaseGoods;
import com.wms.mapper.PurchaseDetailMapper;
import com.wms.mapper.PurchaseMapper;
import com.wms.service.PurchaseService;
import com.wms.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class PurchaseServiceImpl implements PurchaseService {

    private String maxCode;// 采购单最大编码

    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    PurchaseDetailMapper purchaseDetailMapper;

    @Override
    public List<Purchase> getAll() {
        return purchaseMapper.selectAll();
    }

    @Override
    public List<Purchase> queryByCondition(Purchase condition) {
        return purchaseMapper.selectByCondition(condition);
    }

    @Override
    public PageBean queryByPage(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List list= purchaseMapper.selectByPage();
        PageInfo info=new PageInfo(list);
        return new PageBean(page,pageSize,info.getTotal(),list);
    }

    @Override
    public List<PurchaseDetail> queryPurchaseDetail(int id) {
        return purchaseMapper.getPurchaseMaterialsById(id);
    }

    @Override
    public List<PurchaseGoods> queryPurchaseGoods(String purchaseCode) {
        return purchaseMapper.selectPurchaseGoodsByCode(purchaseCode);
    }

    @Transactional
    @Override
    public int savePurchase(Purchase purchase) {
        //设置采购单号
        purchase.setPurchaseCode(getNewCode());
        //保存采购单——失败的情况
        int rs=purchaseMapper.addPurchase(purchase);
        Assert.isZero(rs,"保存采购单失败");
        //保存采购单成功
        if(null!=purchase.getPurchaseMaterials()&&purchase.getPurchaseMaterials().size()>0){
            purchaseDetailMapper.batchSavePurchaseDetail(purchase.getId(),purchase.getPurchaseMaterials());
            Assert.isZero(rs,"保存订单明细失败");
        }
        return rs;
    }

    String getNewCode(){
        //获取系统年份
        String year=getYear();
        //判断内存中是否存在最大编码
        if(null==maxCode){
            //不存在 查询数据库 获取当前年份最大编码
            maxCode=purchaseMapper.getMaxCode(year);
            if(null==maxCode){
                //如果数据库也没有记录，说明当前要增加的是当前年份第一笔采购订单
                maxCode= Prefix.CG+year+"001";
                return maxCode;
            }
        }
        //存在最大编码 计算新的编码 CG25001(截取的时候包含起始位置不包含结束位置)
        //判断最大编码号当前年份是否为同一年
        String tmp=maxCode.substring(2,4);
        if(!year.equals(tmp)){
            //不等 说明是新的一年
            maxCode=Prefix.CG+year+"001";
            return maxCode;
        }
        //如果是同一年
        String seq=maxCode.substring(5);
        int c=Integer.parseInt(seq);
        c++;
        //新的编码
        maxCode=Prefix.CG+year+String.format("%03d",c);//进行补0操作
        return maxCode;
    }

    String getYear(){
        LocalDate now=LocalDate.now();
        int year=now.getYear();
        String yy=String.valueOf(year).substring(2);
        return yy;
    }
}
